package cn.kgc.runner;

import cn.kgc.base.BaseInfoProperties;
import cn.kgc.base.SystemConstant;
import cn.kgc.entity.SysParams;
import cn.kgc.service.SysParamsService;
import cn.kgc.utils.RedisUtil;
import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author YC
 */
@Component
public class ResourceCacheRunner implements CommandLineRunner {

    @Resource
    private SysParamsService sysParamsService;
    @Resource
    private Cache<String, Object> refreshResumeCountCache;
    @Resource
    private RedisUtil redisUtil;

    @Override
    public void run(String... args) throws Exception {
        // 从简历中获取最大刷新次数
        List<SysParams> list = sysParamsService.list();
        int maxResumeRefreshCounts = SystemConstant.MAX_RESUME_REFRESH_COUNTS;
        if (!list.isEmpty()) {
            maxResumeRefreshCounts = list.get(0).getMaxResumeRefreshCounts();
        }
        // 将简历最大刷新次数保存到本地缓存和分布式缓存
        String key = BaseInfoProperties.REDIS_MAX_RESUME_REFRESH_COUNTS;
        refreshResumeCountCache.put(key, maxResumeRefreshCounts);
        //
        redisUtil.set(key, maxResumeRefreshCounts, 20, TimeUnit.DAYS);
    }

}
